﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using justice.Models;
using justice.ViewModel;

namespace justice.Controllers
{
    [Authorize]
    public class BaseInfosController : Controller
    {
        justiceEntities dal = new justiceEntities();
        public ActionResult UserInfosIndex(int? pagenum)
        {
         
            int uid =(int)Session["uid"];
            justice_user rid = dal.justice_user.ToList().Find(l => l.id == uid);
            Role role = dal.Role.ToList().Find(l => l.id == rid.role_id);
            
                if (pagenum == null)
                {
                    pagenum = 1;
                }
                int pagesize = 8;
                int offset = ((int)pagenum - 1) * pagesize;
            if (role.RoleName == "超级管理员")
            {
                decimal usercount = Convert.ToDecimal(dal.justice_user.ToList().Count);
                decimal pagecount = Math.Ceiling(usercount / pagesize);
                var data = from ju in dal.justice_user
                           join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                           from ir in left.DefaultIfEmpty()
                           join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                           from jc in left_.DefaultIfEmpty()
                           join r in dal.Role on ju.role_id equals r.id
                           select new UsersInfos
                           {
                               UserId = ju.id,
                               UserName = ju.userName,
                               UserPwd = ju.password,
                               InsererName = ir.insurerName,
                               JudiciaryName = jc.judiciaryName,
                               RoleName = r.RoleName
                           };
                ViewBag.list = data.ToList().Skip(offset).Take(pagesize);
                ViewBag.pagecount = pagecount;
                ViewBag.pagenum = pagenum;
            }
            else if(role.RoleName=="管理员")
            {
                if (rid.insurer_id != null)
                {
                    int isid =(int)rid.insurer_id;
                    decimal usercount = Convert.ToDecimal(dal.justice_user.ToList().FindAll(l=>l.insurer_id==rid.insurer_id).Count);
                    decimal pagecount = Math.Ceiling(usercount / pagesize);
                    var data = from ju in dal.justice_user
                               join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                               from ir in left.DefaultIfEmpty()
                               join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                               from jc in left_.DefaultIfEmpty()
                               join r in dal.Role on ju.role_id equals r.id
                               where ju.insurer_id == isid
                               select new UsersInfos
                               {
                                   UserId = ju.id,
                                   UserName = ju.userName,
                                   UserPwd = ju.password,
                                   InsererName = ir.insurerName,
                                   JudiciaryName = jc.judiciaryName,
                                   RoleName = r.RoleName
                               };
                    ViewBag.list = data.ToList().Skip(offset).Take(pagesize);
                    ViewBag.pagecount = pagecount;
                    ViewBag.pagenum = pagenum;
                    ViewBag.Role = role.RoleName;
                }
                else
                {
                    int jcid = (int)rid.unit_id;
                    decimal usercount = Convert.ToDecimal(dal.justice_user.ToList().FindAll(l => l.unit_id == rid.unit_id).Count);
                    decimal pagecount = Math.Ceiling(usercount / pagesize);
                    var data = from ju in dal.justice_user
                               join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                               from ir in left.DefaultIfEmpty()
                               join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                               from jc in left_.DefaultIfEmpty()
                               join r in dal.Role on ju.role_id equals r.id
                               where ju.unit_id == jcid
                               select new UsersInfos
                               {
                                   UserId = ju.id,
                                   UserName = ju.userName,
                                   UserPwd = ju.password,
                                   InsererName = ir.insurerName,
                                   JudiciaryName = jc.judiciaryName,
                                   RoleName = r.RoleName
                               };
                    ViewBag.list = data.ToList().Skip(offset).Take(pagesize);
                    ViewBag.pagecount = pagecount;
                    ViewBag.pagenum = pagenum;
                    ViewBag.Role = role.RoleName;
                }
            }
                return View();
        }
        public JsonResult selectorganization(int num)
        {
            if (num == 1)
            {
                return Json(dal.judiciary.ToList());
            }
            else
            {
                return Json(dal.inserer.ToList());
            }
        }
        public ActionResult AddUserIndex()
        {
            int uid = (int)Session["uid"];
            justice_user rid = dal.justice_user.ToList().Find(l => l.id == uid);
            Role role = dal.Role.ToList().Find(l => l.id == rid.role_id);
            if (role.RoleName == "超级管理员")
            {
                ViewBag.insurerlist = dal.inserer.ToList();
                ViewBag.judiciarylist = dal.judiciary.ToList();
                ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName == "管理员");
            }
            else
            {
                ViewBag.insurerlist = dal.inserer.ToList();
                ViewBag.judiciarylist = dal.judiciary.ToList();
                ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员"&&l.RoleName!="管理员");
            }
            
            return View();
        }
        public ActionResult AddUserInfos(string username, string userpwd, int? insurer_id, int? unit_id, int? role_id)
        {
            int uid = (int)Session["uid"];
            justice_user rid = dal.justice_user.ToList().Find(l => l.id == uid);
            Role role = dal.Role.ToList().Find(l => l.id == rid.role_id);
            if (username == "" || userpwd == "" || role_id == null) 
            {
                ModelState.AddModelError("AddUserError", "填写内容不能为空!");
                ViewBag.insurerlist = dal.inserer.ToList();
                ViewBag.judiciarylist = dal.judiciary.ToList();
                if (role.RoleName == "超级管理员")
                {
                    ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName == "管理员");
                }
                else
                {
                    ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员"&&l.RoleName!="管理员");
                }
                
                return View("AddUserIndex");
            }
            else
            {
                justice_user user;
                if (role.RoleName == "超级管理员")
                {
                    if (insurer_id != null)
                    {
                        user = dal.justice_user.ToList().Find(l => l.userName == username || l.insurer_id == insurer_id && l.role_id == role_id);
                    }else
                    {
                        user = dal.justice_user.ToList().Find(l => l.userName == username || l.unit_id == unit_id && l.role_id == role_id);
                    }
                }
                else
                {
                    user = dal.justice_user.ToList().Find(l => l.userName == username);
                }
                    if (user != null)
                    {
                            ViewBag.insurerlist = dal.inserer.ToList();
                            ViewBag.judiciarylist = dal.judiciary.ToList();
                            if (role.RoleName == "超级管理员")
                            {
                                ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName == "管理员");
                                ModelState.AddModelError("AddUserError", "用户名或管理员已存在!");
                            }
                            else
                            {
                                ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员"&&l.RoleName!="管理员");
                                ModelState.AddModelError("AddUserError", "用户名已存在!");
                            }
                        return View("AddUserIndex");
                    }
                    else
                    {
                    if (role.RoleName == "管理员")
                    {
                        int usernum = 0;
                        if (rid.unit_id != null)
                        {
                            usernum = dal.justice_user.ToList().FindAll(l=>l.unit_id==rid.unit_id).Count;
                        }
                        else
                        {
                            usernum = dal.justice_user.ToList().FindAll(l => l.insurer_id == rid.insurer_id).Count;
                        }
                        if (usernum < 7)
                        {
                            justice_user ju_ = new justice_user();
                            ju_.userName = username;
                            ju_.password = userpwd;
                            ju_.insurer_id = rid.insurer_id;
                            ju_.unit_id = rid.unit_id;
                            ju_.role_id = role_id;
                            dal.justice_user.Add(ju_);
                        }
                        else
                        {
                            ViewBag.insurerlist = dal.inserer.ToList();
                            ViewBag.judiciarylist = dal.judiciary.ToList();
                            ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员" && l.RoleName != "管理员");
                            ModelState.AddModelError("AddUserError", "用户人数已达上限!");
                        }
                        
                    }
                    else if(role.RoleName=="超级管理员")
                    {
                        justice_user ju = new justice_user();
                        ju.userName = username;
                        ju.password = userpwd;
                        ju.insurer_id = insurer_id;
                        ju.unit_id = unit_id;
                        ju.role_id = role_id;
                        dal.justice_user.Add(ju);
                    }
                        
                        int num = dal.SaveChanges();
                        if (num > 0)
                        {
                            return RedirectToAction("UserInfosIndex", "BaseInfos");
                        }
                        else
                        {
                            ViewBag.insurerlist = dal.inserer.ToList();
                            ViewBag.judiciarylist = dal.judiciary.ToList();
                            if (role.RoleName == "超级管理员")
                            {
                                ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员");
                            }
                            else
                            {
                                ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员" && l.RoleName != "管理员");
                            }
                            ModelState.AddModelError("AddUserError", "系统错误,添加失败!");
                            return View("AddUserIndex");
                        }
                    }           
            }
        }
        public ActionResult DelUser(int userid)
        {
            justice_user user = dal.justice_user.ToList().Find(l => l.id == userid);
                dal.justice_user.Remove(user);
                dal.SaveChanges();
            return RedirectToAction("UserInfosIndex", "BaseInfos");
        }
        public ActionResult ModifyUserinfos(int uid)
        {
            justice_user user = dal.justice_user.ToList().Find(l => l.id == uid);
            var data = from ju in dal.justice_user
                       join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                       from ir in left.DefaultIfEmpty()
                       join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                       from jc in left_.DefaultIfEmpty()
                       join r in dal.Role on ju.role_id equals r.id
                       select new UsersInfos
                       {
                           UserId = ju.id,
                           UserName = ju.userName,
                           UserPwd = ju.password,
                           InsererName = ir.insurerName,
                           JudiciaryName = jc.judiciaryName,
                           RoleName = r.RoleName
                       };
            UsersInfos ui = data.ToList().Find(l=>l.UserId==user.id);
            ViewBag.rolelist = dal.Role.ToList().FindAll(l => l.RoleName != "超级管理员");
            return View("ModifyUserinfos",ui);
        }
        public ActionResult ModifyUser(int uid,string UserName,string UserPwd,int? role_id)
        {
                justice_user user = dal.justice_user.ToList().Find(l => l.id == uid);
                user.userName = UserName;
                user.password = UserPwd;
                if (role_id != null)
                {
                    user.role_id = role_id;
                }
                dal.SaveChanges();
                return RedirectToAction("UserInfosIndex", "BaseInfos");
        }
        public ActionResult Search(string sewhere,int? pagenum)
        {
            int uid = (int)Session["uid"];
            justice_user rid = dal.justice_user.ToList().Find(l => l.id == uid);
            Role role = dal.Role.ToList().Find(l => l.id == rid.role_id);

            if (pagenum == null)
            {
                pagenum = 1;
            }
            int pagesize = 8;
            int offset = ((int)pagenum - 1) * pagesize;
            if (role.RoleName == "超级管理员")
            {
                decimal usercount = Convert.ToDecimal(dal.justice_user.ToList().FindAll(l=>l.userName.Contains(sewhere)).Count);
                decimal pagecount = Math.Ceiling(usercount / pagesize);
                var data = from ju in dal.justice_user
                           join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                           from ir in left.DefaultIfEmpty()
                           join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                           from jc in left_.DefaultIfEmpty()
                           join r in dal.Role on ju.role_id equals r.id
                           select new UsersInfos
                           {
                               UserId = ju.id,
                               UserName = ju.userName,
                               UserPwd = ju.password,
                               InsererName = ir.insurerName,
                               JudiciaryName = jc.judiciaryName,
                               RoleName = r.RoleName
                           };
                ViewBag.list = data.ToList().FindAll(l => l.UserName.Contains(sewhere)).Skip(offset).Take(pagesize);
                ViewBag.pagecount = pagecount;
                ViewBag.search = sewhere;
                ViewBag.pagenum = pagenum;
            }
            else if (role.RoleName == "管理员")
            {
                if (rid.insurer_id != null)
                {
                    int isid = (int)rid.insurer_id;
                    decimal usercount = Convert.ToDecimal(dal.justice_user.ToList().FindAll(l => l.insurer_id == rid.insurer_id&&l.userName.Contains(sewhere)).Count);
                    decimal pagecount = Math.Ceiling(usercount / pagesize);
                    var data = from ju in dal.justice_user
                               join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                               from ir in left.DefaultIfEmpty()
                               join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                               from jc in left_.DefaultIfEmpty()
                               join r in dal.Role on ju.role_id equals r.id
                               where ju.insurer_id == isid
                               select new UsersInfos
                               {
                                   UserId = ju.id,
                                   UserName = ju.userName,
                                   UserPwd = ju.password,
                                   InsererName = ir.insurerName,
                                   JudiciaryName = jc.judiciaryName,
                                   RoleName = r.RoleName
                               };
                    ViewBag.list = data.ToList().FindAll(l=>l.UserName.Contains(sewhere)).Skip(offset).Take(pagesize);
                    ViewBag.pagecount = pagecount;
                    ViewBag.pagenum = pagenum;
                    ViewBag.search = sewhere;
                    ViewBag.Role = role.RoleName;
                }
                else
                {
                    int jcid = (int)rid.unit_id;
                    decimal usercount = Convert.ToDecimal(dal.justice_user.ToList().FindAll(l => l.unit_id == rid.unit_id&&l.userName.Contains(sewhere)).Count);
                    decimal pagecount = Math.Ceiling(usercount / pagesize);
                    var data = from ju in dal.justice_user
                               join ir in dal.inserer on ju.insurer_id equals ir.insurer_id into left
                               from ir in left.DefaultIfEmpty()
                               join jc in dal.judiciary on ju.unit_id equals jc.id into left_
                               from jc in left_.DefaultIfEmpty()
                               join r in dal.Role on ju.role_id equals r.id
                               where ju.unit_id == jcid
                               select new UsersInfos
                               {
                                   UserId = ju.id,
                                   UserName = ju.userName,
                                   UserPwd = ju.password,
                                   InsererName = ir.insurerName,
                                   JudiciaryName = jc.judiciaryName,
                                   RoleName = r.RoleName
                               };
                    ViewBag.list = data.ToList().FindAll(l=>l.UserName.Contains(sewhere)).Skip(offset).Take(pagesize);
                    ViewBag.pagecount = pagecount;
                    ViewBag.pagenum = pagenum;
                    ViewBag.search = sewhere;
                    ViewBag.Role = role.RoleName;
                }
            }
            return View("UserInfosIndex");
        }
    }
}